﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WellDone
{
    public partial class EditMenuGroupDialog : ChildWindow
    {
        public ServiceReferenceOpenRPOS.MenuGroup selectedMenuGroup;
        public string groupName = "";
        public EditMenuGroupDialog()
        {
            InitializeComponent();
        }

        private async void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (btnOK.IsEnabled)
            {
                btnOK.IsEnabled = false;
                if (tbMenuGroupName.Text.Length > 0)
                {
                    // Update the name of this menu category
                    if (groupName.Equals(tbMenuGroupName.Text.Trim(),StringComparison.CurrentCultureIgnoreCase)==false)
                    {
                        await Globals.db.GetMenuGroupObjects("Name='" + tbMenuGroupName.Text.Trim() + "'");
                        if (Globals.db.MenuGroupRecords.Count > 0)
                        {
                            MessageBox.Show("Menu group is already exists");
                            btnOK.IsEnabled = true;
                            return;
                        }
                        groupName = tbMenuGroupName.Text.Trim();
                    }                    
                    selectedMenuGroup.col_Name = tbMenuGroupName.Text.Trim();
                    selectedMenuGroup.bFilterName = true;
                    selectedMenuGroup.col_Visible = (cbMenuGroupVisible.IsChecked == true);
                    selectedMenuGroup.bFilterVisible = true;
                    await Globals.db.UpdateMenuGroupObject(selectedMenuGroup);
                    Globals.menuItems.initMenuGroups(true);                    
                    this.DialogResult = true;
                }
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbMenuGroupName_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter)
                OKButton_Click(sender, e);
        }
    }
}

